package com.lottery.pay.service.impl;

import com.alipay.api.AlipayApiException;
import com.lottery.common.contants.Constants;
import com.lottery.common.contants.ReQueryResult;
import com.lottery.common.dao.PaymentRecordsMapper;
import com.lottery.common.entity.PaymentRecords;
import com.lottery.common.service.OrderInfoService;
import com.lottery.pay.service.AliPayService;
import com.lottery.pay.service.BalanceService;
import com.lottery.pay.service.PaymentRecordsService;
import com.lottery.pay.service.WeixinService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Date;

import static com.lottery.common.contants.Constants.*;

@Service
@Slf4j
public class PaymentRecordsServiceImpl  extends ServiceImpl<PaymentRecordsMapper, PaymentRecords>  implements PaymentRecordsService {
    private ReQueryResult result = new ReQueryResult();
    @Value("networking")
    private String networking;
    @Resource
    private PaymentRecordsMapper paymentRecordsMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private AliPayService aliPayService;
    @Autowired
    private WeixinService weixinService;
    @Autowired
    private BalanceService balanceService;
    @Autowired
    private OrderInfoService orderInfoService;

    @Override
    public int insertSelective(PaymentRecords record) {
        return paymentRecordsMapper.insert(record);
    }

    @Override
    public PaymentRecords selectByPrimaryKey(Long id) {
        return paymentRecordsMapper.selectById(id);
    }

    @Override
    public int updateByPrimaryKeySelective(PaymentRecords record) {
        log.info("updateByPrimaryKeySelective");
        QueryWrapper<PaymentRecords> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("order_num",record.getOrderNum()).last(" limit 1");
        PaymentRecords records=paymentRecordsMapper.selectOne(queryWrapper);
        record.setId(records.getId());
        return paymentRecordsMapper.updateById(record);
    }



    /**
     * 统一支付接口
     * @param records
     * @return
     */
    @Override
    public ReQueryResult payAll(PaymentRecords records) throws AlipayApiException {
        result = new ReQueryResult();
        result.error(Constants.PARAMS_WRONG_DESC);

        if ("0".equals(networking)){
            records.setTotalFee(BigDecimal.ONE);
        }

//        records.setCreateTime(new Date());
        records.setStatus(Constants.TYPE_ONE);


        //需要继续支付能力时 才需查询是否已创建支付记录
//        PaymentRecords param = new PaymentRecords();
//        param.setOrderNum(records.getOrderNum());
//        QueryWrapper<PaymentRecords> queryWrapper = new QueryWrapper<>(param);
//        Integer count=paymentRecordsMapper.selectCount(queryWrapper);
//        if (count<=0) {
//            count=paymentRecordsMapper.insert(records);
//        }
//        if (count<=0){
//            result.error(Constants.PAY_ORDER_ERROR);
//            return  result;
//        }
        log.info("进入统一支付处理，type {}",records.getType());
        //余额支付
        if (records.getType()==TYPE_THREE){
            result=balanceService.createPayment(records);
            if (result.getResultCode().equals(TYPE_ZERO_ZERO_STR)){
                result=orderInfoService.synthesizeOrder(records);
            }
        }
        //免费额度支付
        if (records.getType()==TYPE_FOUR){
            result=balanceService.createGoldePayment(records);
            if (result.getResultCode().equals(TYPE_ZERO_ZERO_STR)){
                result=orderInfoService.synthesizeOrder(records);
            }
        }

        //支付宝支付
        if (records.getType()==TYPE_ONE){
            result=aliPayService.createPayment(records);
        }

        //微信支付
        if (records.getType()==TYPE_TWO){
            result=weixinService.createPayment(records);
        }
        return result;
    }



    /**
     * 统一支付查询接口
     * @param records
     * @param session
     * @param request
     * @return
     */
    @Override
    public ReQueryResult queryAll(PaymentRecords records, HttpSession session, HttpServletRequest request) throws AlipayApiException {
        result = new ReQueryResult();
        result.error(Constants.PARAMS_WRONG_DESC);


        //余额支付
        if (records.getType()==TYPE_THREE){
            result=balanceService.queryPayment(records);
        }

        //支付宝支付
        if (records.getType()==TYPE_ONE){
            result=aliPayService.queryPayment(records);
        }

        //微信支付
        if (records.getType()==TYPE_TWO){
            result=weixinService.queryPayment(records);
        }

        result.success(Constants.SUCCESS_DESC);
        return result;
    }


    /**
     * 统一支付回调接口
     *  @param type 支付类型
     * @param request
     * @return
     */
    @Override
    public ReQueryResult resultAll(Integer type, HttpServletRequest request) throws AlipayApiException {
        result = new ReQueryResult();
        result.error(Constants.PARAMS_WRONG_DESC);
        log.info("type is {}",type);

        //余额支付
//        if (records.getType()==0){
//            result=balancePay(records,session,request);
//        }

        //支付宝支付
        if (type==TYPE_ONE){
            result=aliPayService.resultDispose(request);
        }
//
//        //微信支付
        if (type==TYPE_TWO){
            result=weixinService.resultDispose(request);
        }
        return result;
    }





}
